package com.kunlun.basedata.organize.controller;

import com.kunlun.basedata.organize.model.ApplicationModel;
import com.kunlun.basedata.organize.model.vo.ApplicationVo;
import com.kunlun.basedata.organize.service.IApplicationService;
import com.kunlun.common.model.Page;
import com.kunlun.common.utils.ResponseUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * 部门Controller类
 */
@RestController
@RequestMapping(value = "/application")
public class ApplicationController {

    private Logger logger = LogManager.getLogger();

    @Autowired
    private IApplicationService applicationService;

    @RequestMapping(value = "/getApplicationList", method = RequestMethod.GET)
    public Object getApplicationList(ApplicationModel applicationModel) {
        try {
            List<ApplicationVo> list = applicationService.getApplicationList(applicationModel);
            return ResponseUtil.successResponse(list);
        } catch (Exception e) {
            logger.error("ApplicationController getApplicationList Error: ", e);
            return ResponseUtil.failedResponse("获取部门数据失败！", e.getMessage());
        }
    }

    @RequestMapping(value = "/getApplicationPage", method = RequestMethod.GET)
    public Object getApplicationPage(ApplicationModel ApplicationModel, int currentPage, int pageSize) {
        try {
            Page<ApplicationModel> page = applicationService.getApplicationPage(ApplicationModel, currentPage, pageSize);
            return ResponseUtil.successResponse(page);
        } catch (Exception e) {
            logger.error("ApplicationController getApplicationPage Error: ", e);
            return ResponseUtil.failedResponse("获取部门数据失败！", e.getMessage());
        }
    }

    @RequestMapping(value = "/addApplication", method = RequestMethod.POST)
    public Object addApplication(ApplicationModel ApplicationModel) {
        try {
            applicationService.addApplication(ApplicationModel);
            return ResponseUtil.successResponse("SUCCESS");
        } catch (Exception e) {
            logger.error("ApplicationController addApplication Error: ", e);
            return ResponseUtil.failedResponse("添加部门失败！", e.getMessage());
        }
    }

    @RequestMapping(value = "/updateApplication", method = RequestMethod.POST)
    public Object updateApplication(ApplicationModel ApplicationModel) {
        try {
            applicationService.updateApplication(ApplicationModel);
            return ResponseUtil.successResponse("SUCCESS");
        } catch (Exception e) {
            logger.error("ApplicationController updateApplication Error: ", e);
            return ResponseUtil.failedResponse("更新部门失败！", e.getMessage());
        }
    }

    @RequestMapping(value = "/deleteApplication", method = RequestMethod.POST)
    public Object deleteApplication(String ids) {
        try {
            List<String> list = Arrays.asList(ids.split(","));
            applicationService.deleteApplication(list);
            return ResponseUtil.successResponse("SUCCESS");
        } catch (Exception e) {
            logger.error("ApplicationController deleteApplication Error: ", e);
            return ResponseUtil.failedResponse("删除部门失败！", e.getMessage());
        }
    }
}
